// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/functions/macros.h>

namespace kumo::pollux::functions::sparksql {
    /// array_append(array(E), element) -> array(E)
    /// Given an array and another element, append the element at the end of the
    /// array.
    template<typename TExec>
    struct ArrayAppendFunction {
        POLLUX_DEFINE_FUNCTION_TYPES(TExec);

        MELON_ALWAYS_INLINE bool callNullable(
            out_type<Array<Generic<T1> > > &out,
            const arg_type<Array<Generic<T1> > > *array,
            const arg_type<Generic<T1> > *element) {
            if (array == nullptr) {
                return false;
            }
            out.reserve(array->size() + 1);
            out.add_items(*array);
            element ? out.push_back(*element) : out.add_null();
            return true;
        }
    };
} // namespace kumo::pollux::functions::sparksql
